﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.IO;

namespace ClassDB
{
    public class DBTrans
    {
        public static DataTable dgv2dt(DataGridView dgv)
        {
            DataTable dt = new DataTable();
            dt.TableName = dgv.Name;
            DataColumn dc;
            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                dc = new DataColumn();
                dc.ColumnName = dgv.Columns[i].HeaderText.ToString();
                dt.Columns.Add(dc);
            }
            for (int j = 0; j < dgv.Rows.Count; j++)
            {
                DataRow dr = dt.NewRow();
                for (int x = 0; x < dgv.Columns.Count; x++)
                {
                    dr[x] = dgv.Rows[j].Cells[x].Value;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }

        public static void dgv2XML(DataGridView dgv, string FileName)
        {
            DataTable dt = dgv2dt(dgv);
            if (File.Exists(FileName))
            {
                File.Delete(FileName);
            }
            dt.WriteXml(FileName, System.Data.XmlWriteMode.WriteSchema);
        }

        public static DataTable XML2dgv(string FileName, DataGridView dgv)
        {
            DataTable dt = new DataTable();
            dt.ReadXml(FileName);            
            dgv.DataSource = dt;
            return dt;
        }
    }
}
